<?php

namespace App\Controller\Wechat;

use App\Base\BaseCache;
use App\Base\BaseController4App;
use App\Base\Format;
use App\Entity\User;
use App\Model\LazyCommon\MiniAppModel;
use App\Model\LazyCommon\UserModel;
use App\Model\TopicModel;
use App\Model\UserLiveModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("/wechat_mini")
 */
class MiniAppController extends BaseController4App
{


    /**
     * @route("/init",name="WechatMiniAppInit")
     */
    public function WechatMiniAppInit()
    {
        if(!$code = $this->getParamRequired("code")){return $this->json_out(0,"未登录");}
        $MiniAppModel = new MiniAppModel($this->getDoctrine());
        $rst = $MiniAppModel->getOpenidAndAccessToken4Mini($code);
        //$rst = "oHSdp5FwwtlspPaPqcxCg5yPU9yc";
//        var_dump($rst);
        if ($rst) {
            //启用Cache
            $BaseCache = new BaseCache();
            $access_token = $BaseCache->getUserAccessToken(Request::createFromGlobals());;


            //检查用户是否存在如果存在就登录ID，不存在就创建用户
            $UserModel = new UserModel($this->getDoctrine());
            $user = $UserModel->getByWeChetId($MiniAppModel->Openid);


            if ($user->getAccount()) {
                $access_token =  $user->getAccount();
            }else{
                $user->setAccount($access_token);
            }
            $sessionData = $UserModel->createSessionData($user);
            $user->setLastLoginTime(time());
            $this->myEm()->persist($user);
            $this->myEm()->flush();

            $BaseCache->createUserToken($access_token,$sessionData);
            return $this->json_out(1,"登陆成功",$access_token);
        } else {
            return $this->json_out(0,"系统异常",$rst);
        }
    }

    /**
     * @route("/login",name="WechetMiniLogin")
     */
    public function WechetMiniLogin()
    {
        if(!$code = $this->getParamRequired("code")){return $this->json_out(0,"Code is missed");}
        $MiniAppModel = new MiniAppModel($this->getDoctrine());
        $rst = $MiniAppModel->getOpenidAndAccessToken4Mini($code);
        //$rst = "oHSdp5FwwtlspPaPqcxCg5yPU9yc";
        if ($rst) {
            //检查用户是否存在如果存在就登录ID，不存在就创建用户
            $UserModel = new UserModel($this->getDoctrine());
            $user = $UserModel->getByWeChetId($MiniAppModel->Openid);
            $sessionData = $UserModel->createSessionData($user);
            $user->setLastLoginTime(time());
            $this->myEm()->persist($user);
            $this->myEm()->flush();
            //启用Cache
            $BaseCache = new BaseCache();
            $access_token = $BaseCache->getUserAccessToken(Request::createFromGlobals());;
            $BaseCache->createUserToken($access_token,$sessionData);
            return $this->json_out(1,"登陆成功",$this->data);
        } else {
            return $this->json_out(0,"系统异常",$this->data);
        }
    }


}
